package org.celery.modules.user.service.impl;

import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import lombok.AllArgsConstructor;
import org.celery.modules.user.entity.UserDomain;
import org.celery.modules.user.vo.UserDomainVO;
import org.celery.modules.user.mapper.UserDomainMapper;
import org.celery.modules.user.service.IUserDomainService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springblade.core.tool.utils.Func;
import org.springblade.modules.system.entity.User;
import org.springblade.modules.system.service.IUserService;
import org.springframework.stereotype.Service;
import com.baomidou.mybatisplus.core.metadata.IPage;

import java.util.List;

/**
 * 用户与域名关联表 服务实现类
 *
 * @author Celery
 * @since 2022-04-06
 */
@Service
@AllArgsConstructor
public class UserDomainServiceImpl extends ServiceImpl<UserDomainMapper, UserDomain> implements IUserDomainService {

	private final IUserService userService;

	@Override
	public IPage<UserDomainVO> selectUserDomainPage(IPage<UserDomainVO> page, UserDomainVO userDomain) {
		return page.setRecords(baseMapper.selectUserDomainPage(page, userDomain));
	}

	@Override
	public User selectUserByDomain(String domain) {
		List<UserDomain> userDomainList = list(Wrappers.<UserDomain>lambdaQuery()
				.eq(UserDomain::getDomain, domain)
				.orderByAsc(UserDomain::getId)
		);
		if (Func.isNotEmpty(userDomainList)) {
			return userService.getById(userDomainList.get(0).getUserId());
		}
		return null;
	}

}
